package com.mgq.java8.lambda;

import java.util.HashMap;
import java.util.Map;
import java.util.function.*;

/**
 * <b>功能：</b>Supplier<br>
 * <b>Copyright mgq</b>
 * <li>版本&nbsp;&nbsp;&nbsp;&nbsp;修改日期&nbsp;&nbsp;&nbsp;&nbsp;部　　门&nbsp;&nbsp;&nbsp;&nbsp;作　者&nbsp;&nbsp;&nbsp;&nbsp;变更内容</li><hr>
 * <li>v1.0&nbsp;&nbsp;&nbsp;&nbsp;20220412&nbsp;&nbsp;营业后台&nbsp;&nbsp;&nbsp;&nbsp;马广奇&nbsp;&nbsp;&nbsp;&nbsp;创建类</li>
 * <br><br>
 *
 * @author 马广奇
 * @version 2022-04-12 20:56
 */
public class SupplierTest {
    /**
     * 测试Supplier
     */
    public static void test(int x, int y) {
        Supplier<Apple> appleSupplier = () -> new Apple("red", 2);
        Apple apple = appleSupplier.get();

        //构造函数有参数时,方法引用的使用
        BiFunction<String, Integer, Apple> appleNew = Apple::new;
        appleNew.apply("blue", 30);

        //等价于下面的写法
        BiFunction<String, Integer, Apple> appleNew2 =(color,weight) -> new Apple(color, weight);
        appleNew2.apply("blue", 30);




        //java8提供了: BooleanSupplier,IntSupplier, LongSupplier,DoubleSupplier
        //BooleanSupplier 用于接收lambda表达式所返回的boolean值结果
        BooleanSupplier booleanSupplier = () -> true;
        BooleanSupplier booleanSupplier2 = () -> x > y;

        IntSupplier intSupplier = () -> x;
        System.out.println("intSupplier:" + intSupplier.getAsInt());
        System.out.println(booleanSupplier.getAsBoolean());
        System.out.println(booleanSupplier2.getAsBoolean());

        System.out.println(apple.getColor());
    }
    static  Map<String, Function<String, Fruit>> map = new HashMap<>();
    static {
        map.put("apple", Apple::new);
        map.put("orange", Orange::new);
        // etc...
    }
    public static Fruit giveMeFruit(String fruit,int weight) {
        //再举例
        //不将构造函数实例化却能够引用它，这个功能有一些有趣的应用

        return map.get(fruit.toLowerCase())
                .apply(String.valueOf(weight));
    }

    public static void main(String[] args) {
        test(2, 3);
    }
}
